<script src="../node_modules/vue/dist/vue.js"></script>

<script>
  const RulesPlugin = {
    install(Vue) {
      Vue.mixin({
        created() {
          const rules = this.$options.rules
          if (rules) {
            Object.keys(rules).forEach(key => {
              const { validate, message } = rules[key];
              if (validate) {
                // this.$watch
                this.unwatch = this.$watch(key, newValue => {
                  const valid = validate(newValue)
                  if (!valid) {
                    console.log(message)
                  }
                })
              }

            })
          }
        },
        destroy() {
          this.unwatch()
        }
      })
    }
  }

  Vue.use(RulesPlugin)
</script>